<template>
  <page>
    <template v-slot:header>
      <h2>
        <i class="el-icon-s-custom"></i>
        <span>{{ $t("user.role_manager") }}</span>
      </h2>
    </template>
    <template v-slot:content>
      <el-dialog
        :title="title"
        :visible.sync="display"
        :before-close="handleClose"
      >
        <create-or-update
          @click="handleClose"
          v-if="type === 'update' || type === 'create' || type === 'copy'"
          :type="type"
          :rowId="id"
        ></create-or-update>
        <menu-form @click="handleClose" v-else-if="type==='menu'" :rowId="id"></menu-form>
        <operation-form @click="handleClose" v-else-if="type==='operation'" :rowId="id"></operation-form>
      </el-dialog>
      <ky-table
        :search_data="search_data"
        :getData="getRoles"
        :showSelect="showSelect"
        :search_criteria="search_criteria"
        ref="table"
      >
        <template v-slot:table-action>
          <authorization-button class="kylin-item-button" @click="handleAdd" name="role:create"
            >{{ $t("configure.add") }}
          </authorization-button>
        </template>
        <template v-slot:table>
          <el-table-column prop="name" :label="$t('user.role_name')">
          </el-table-column>
          <el-table-column
            prop="description"
            :label="$t('user.role_description')"
          >
          </el-table-column>
          <el-table-column :label="$t('user.authorization')">
            <template slot-scope="scope">
              <authorization-button
                @click.native.prevent="handleMenu(scope.row)"
                type="text"
                size="medium"
                name="role:menu"
              >
                {{ $t("user.authorization_menu") }}
              </authorization-button>
              <authorization-button
                @click.native.prevent="handleOperation(scope.row)"
                type="text"
                size="medium"
                name="role:operation"
              >
                {{ $t("user.authorization_operation") }}
              </authorization-button>
            </template>
          </el-table-column>
          <el-table-column :label="$t('action')">
            <template slot-scope="scope">
              <authorization-button
                @click.native.prevent="handleCopy(scope.row)"
                type="text"
                size="medium"
                name="role:create"
              >
                {{ $t("copy") }}
              </authorization-button>
              <authorization-button
                @click.native.prevent="handleEdit(scope.row)"
                type="text"
                size="medium"
                name="role:update"
              >
                {{ $t("edit") }}
              </authorization-button>
              <el-popconfirm
                :title="$t('user.role_delete_confirm')"
                @confirm="handleDelete(scope.row)"
                icon-color="red"
                cancel-button-type="default"
                confirm-button-type="danger"
              >
                <authorization-button
                  type="text"
                  slot="reference"
                  size="medium"
                  class="kylin-delete"
                  name="role:delete"
                >
                  {{ $t("delete") }}
                </authorization-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </ky-table>
    </template>
  </page>
</template>

<script>
import Page from "@/components/Page";
import kyTable from "@/components/KyTable";
import CreateOrUpdate from "./form/CreateUpdateForm.vue";
import { getRoles, deleteRole } from "@/api/role";
import MenuForm from "./form/menuForm.vue"
import OperationForm from './form/operationForm.vue';
import AuthorizationButton from "@/components/AuthorizationButton"
export default {
  name: "roles",
  components: {
    Page,
    kyTable,
    CreateOrUpdate,
    OperationForm,
    MenuForm,
    AuthorizationButton
  },
  activated() {
      this.$refs.table.handleSearch();
  },
  data() {
    return {
      display: false,
      showSelect: false,
      search_criteria: {
        sortBy: "id",
        sortOrder: "desc",
      },
      id: "",
      title: "",
      type: "",
      delete_content: "",
      search_data: {
        name: {
          name: "user.role_name",
          type: "text",
        },
        description: {
          name: "description",
          type: "text",
        },
      },
    };
  },
  methods: {
    getRoles,
    handleClose(type) {
      this.display = false;
      this.title = "";
      this.type = "";
      this.id = "";
      if (type === "success") {
        this.$refs.table.handleSearch();
      }
    },
    handleAdd() {
      this.display = true;
      this.title = this.$t("configure.add");
      this.type = "create";
    },
    handleEdit(row) {
      this.id = row.id;
      this.display = true;
      this.title = this.$t("edit");
      this.type = "update";
    },
    handleCopy(row) {
      this.id = row.id;
      this.display = true;
      this.title = this.$t("copy");
      this.type = "copy";
    },
    handleMenu(row) {
        this.type = "menu";
        this.display = true;
        this.id = row.id;
        this.title = this.$t("user.authorization_menu")
    },
    handleOperation(row) {
        this.type = "operation";
        this.display = true;
        this.id = row.id;
        this.title = this.$t("user.authorization_operation")
    },
    handleDelete(row) {
      deleteRole({ id: row.id }).then((res) => {
        if (res.data.code == "0") {
          this.$message({
            type: "success",
            message: res.data.message,
          });
          this.$refs.table.handleSearch();
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.size {
  width: 134px;
}
.label {
  font-weight: 600;
  margin-right: 10px;
}
</style>